import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {ModalController} from '@ionic/angular';
import {InterfaceService} from '@services/interface.service';
import Utils from '@const/Utils';
import {CommonService} from '@services/common.service';

@Component({
    selector: 'app-start',
    templateUrl: './start.page.html',
    styleUrls: ['./start.page.scss'],
})
export class StartPage implements OnInit {

    condition: string = '';
    startOrders: any[] = [];
    page_num: number = 0;
    isEnd: boolean = false;
    loading: boolean = false;

    constructor(private router: Router, private modalCtrl: ModalController,
                private ifs: InterfaceService,
                private common: CommonService) {
    }

    new() {
        this.router.navigateByUrl('/tabs/start/start-new');
    }

    ngOnInit() {
    }

    tail(item) {

    }

    goContract() {

    }

    search() {
        this.common.loading();
        this.page_num = 1;
        this.startOrders = [];
        this.searchStartOrder().then(v => this.common.hideloading()).catch(e => this.common.hideloading());
    }

    searchStartOrder() {
        return new Promise((resolve, reject) => {
            this.ifs.searchStartOrder({page_num: this.page_num, condition: this.condition}).then(v => {
                let startOrders = v['startOrders'] || [];
                if (Utils.isNull(startOrders)) {
                    this.isEnd = true;
                }
                Array.prototype.push.apply(this.startOrders, startOrders);
                this.loading = false;
                resolve(v);
            }).catch(e => {
                this.loading = false;
                reject(e);
            });
        });
    }
}
